package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.excel.ImportExcelUtil;
import com.ruoyi.system.domain.TmcHouseBill;
import com.ruoyi.system.domain.TmcHouseInfo;
import com.ruoyi.system.domain.TmcHouseUser;
import com.ruoyi.system.domain.TmcPay;
import com.ruoyi.system.mapper.TmcHouseBillMapper;
import com.ruoyi.system.mapper.TmcHouseUserMapper;
import com.ruoyi.system.service.TmcHouseUserService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TmcHouseUserServiceImpl implements TmcHouseUserService {
    @Autowired
    private TmcHouseUserMapper tmcHouseUserMapper;
    @Autowired
    private TmcHouseBillMapper tmcHouseBillMapper;

    @Override
    public TmcHouseUser selectTmcHouseUserById(Long id) {
        return tmcHouseUserMapper.selectTmcHouseUserById(id);
    }

    @Override
    public List<TmcHouseUser> selectTmcHouseUserList(TmcHouseUser tmcHouseUser) {
        return tmcHouseUserMapper.selectTmcHouseUserList(tmcHouseUser);
    }

    @Override
    public int insertTmcHouseUser(TmcHouseUser tmcHouseUser) {
        
        return tmcHouseUserMapper.insertTmcHouseUser(tmcHouseUser);
    }

    @Override
    public int updateTmcHouseUser(TmcHouseUser tmcHouseUser) {
        return tmcHouseUserMapper.updateTmcHouseUser(tmcHouseUser);
    }

    @Override
    public int deleteTmcHouseUserById(Long id) {
        return tmcHouseUserMapper.deleteTmcHouseUserById(id);
    }

    @Override
    public int deleteTmcHouseUserByIds(Long[] ids) {
        return tmcHouseUserMapper.deleteTmcHouseUserByIds(ids);
    }

    @Override
    public TmcHouseInfo selectTmcHouseInfoById(Long id) {
        return tmcHouseUserMapper.selectTmcHouseInfoById(id);
    }

    @Override
    public List<TmcHouseInfo> selectTmcHouseInfoList(TmcHouseInfo tmcHouseInfo) {
        List<TmcHouseInfo> tmcHouseInfos = tmcHouseUserMapper.selectTmcHouseInfoList(tmcHouseInfo);
        for (int i = 0; i < tmcHouseInfos.size(); i++) {
            TmcHouseUser tmcHouseUser = new TmcHouseUser();
            tmcHouseUser.setHouseId(tmcHouseInfos.get(i).getId());
            tmcHouseInfos.get(i).setTmcHouseUserList(tmcHouseUserMapper.selectTmcHouseUserList(tmcHouseUser));
        }
        return tmcHouseInfos;
    }

    @Override
    public int insertTmcHouseInfo(TmcHouseInfo tmcHouseInfo) {
        tmcHouseInfo.setCreateTime(DateUtils.getNowDate());
        return tmcHouseUserMapper.insertTmcHouseInfo(tmcHouseInfo);
    }

    @Override
    public int updateTmcHouseInfo(TmcHouseInfo tmcHouseInfo) {
        tmcHouseInfo.setUpdateTime(DateUtils.getNowDate());
        return tmcHouseUserMapper.updateTmcHouseInfo(tmcHouseInfo);
    }

    @Override
    public int deleteTmcHouseInfoById(Long id) {
        return tmcHouseUserMapper.deleteTmcHouseInfoById(id);
    }

    @Override
    public int deleteTmcHouseInfoByIds(Long[] ids) {
        return tmcHouseUserMapper.deleteTmcHouseInfoByIds(ids);
    }

    @Override
    public int importExcel(MultipartFile file) {
        String fileName = file.getName();
        List<String[]> dataList = null;
        String headData = null;
        Workbook workbook = null;
        String dateFormat = "yyyy-MM-dd";
        boolean t = true;
        int a = 0;
        try {
            //部门
            workbook = new XSSFWorkbook(file.getInputStream());
            if (workbook != null) {
                headData = ImportExcelUtil.getExcelHeadData(workbook);
                dataList = ImportExcelUtil.getExcelData(workbook, 0, 1, dateFormat);
            }
            //拿到数据进行数据库操作
            for (int j = 1; j < dataList.size(); j++) {//行
                if (!"".equals(dataList.get(j)[0]) && null != dataList.get(j)[0]) {
                    String[] strings = dataList.get(j);
                    String[] names = Convert.toStrArray("/", strings[0]);//对象基数
//                房屋信息
                    TmcHouseInfo houseInfo = new TmcHouseInfo();
                    houseInfo.setCompanyName(dataList.get(j)[2]);
                    houseInfo.setUnitname(dataList.get(j)[3]);
                    houseInfo.setArea(Double.valueOf(dataList.get(j)[5]));
                    houseInfo.setHousenum(dataList.get(j)[4]);
                    tmcHouseUserMapper.insertTmcHouseInfo(houseInfo);
                    //对象基数
                    //产生几个对象
                    if (names.length > 1) {
                        String[] tels;
                        if (!"".equals(dataList.get(j)[1]) && null != dataList.get(j)[1]) {
                            tels = Convert.toStrArray("/", strings[1]);
                        } else {
                            tels = new String[]{""};
                        }
                        //多条记录
                        for (int i = 0; i < names.length; i++) {
                            TmcHouseUser user = new TmcHouseUser();
                            user.setHouseId(houseInfo.getId());
                            if (!"".equals(names[i]) && null != names[i]) {
                                //名字
                                user.setOwnerName(names[i]);
                                //电话
                                if (tels.length > i) {
                                    user.setOwnerPhone(tels[i]);
                                } else {
                                    user.setOwnerPhone("");
                                }
                            }
                            tmcHouseUserMapper.insertTmcHouseUser(user);
                        }
                    } else {
//                      房屋所属人
                        TmcHouseUser user = new TmcHouseUser();
                        user.setHouseId(houseInfo.getId());
                        user.setOwnerName(dataList.get(j)[0]);
                        if (!"".equals(dataList.get(j)[1]) && null != dataList.get(j)[1]) {
                            String[] strings1 = Convert.toStrArray("/", dataList.get(j)[1]);
                            user.setOwnerPhone(strings1[0]);
                        } else {
                            user.setOwnerPhone("");
                        }
                        tmcHouseUserMapper.insertTmcHouseUser(user);
                    }
                }
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
        }
    }

    @Override
    public TmcHouseBill selectTmcHouseBillById(Long id) {
        TmcHouseBill tmcHouseBill = tmcHouseUserMapper.selectTmcHouseBillById(id);
//        tmcHouseBill.setTmcHouseSubbill(tmcHouseUserMapper.selectTmcHouseSubbillById(tmcHouseBill.getId()));
        return tmcHouseBill;
    }
    @Override
    public int updateNum(String out_trade_no) {
        return tmcHouseUserMapper.updateNum(out_trade_no);
    }

    @Override
    public int selectCarnum(String ownerphone, Long companyId) {
        HashMap map = new HashMap();
        map.put("ownerphone", ownerphone);
        map.put("companyId", companyId);
        return tmcHouseUserMapper.selectCarnum(map);
    }

    @Override
    public int updateUserHouseBillInfo(String out_trade_no) {
        return tmcHouseUserMapper.updateUserHouseBillInfo(out_trade_no);
    }

}
